{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67ce3338-182c-4d0d-815f-8eec476824d5",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "import sys\n",
    "sys.path.append('./utils')\n",
    "sys.path.append('./utils/APIs')\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import torch\n",
    "import timm\n",
    "import argparse\n",
    "from Config import config\n",
    "from utils.common import read_from_file, save_model, write_to_file, train_val_split\n",
    "from utils.DataProcess import Processor\n",
    "from Trainer import Trainer\n",
    "from PreTrainer import PreTrainer\n",
    "from Models.OTEModel import Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7f93789f-6168-4e81-ac93-add5171d5331",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TextModel: huawei-noah/TinyBERT_General_4L_312D, ImageModel: WAVM, FuseModel: CMAT\n"
     ]
    }
   ],
   "source": [
    "# args\n",
    "# 训练模型\n",
    "do_train = False\n",
    "# 预测测试集数据\n",
    "do_test = True\n",
    "# 已经训练好的模型路径\n",
    "load_model_path = './save_models/CMAT/pytorch_model.bin'\n",
    "# 设置学习率（后面会通过GRW-DSA寻找最优学习率）\n",
    "lr = 3e-6\n",
    "# 设置权重衰减\n",
    "weight_decay = 1e-2\n",
    "# 设置训练轮数\n",
    "epoch = 30\n",
    "# 文本分析模型\n",
    "text_pretrained_model = 'huawei-noah/TinyBERT_General_4L_312D'\n",
    "cv_pretrained_model = 'WAVM'\n",
    "# 融合模型类别\n",
    "fuse_model_type = 'CMAT'\n",
    "# 仅用文本预测\n",
    "text_only = False\n",
    "# 仅用图像预测\n",
    "img_only = False\n",
    "\n",
    "config.learning_rate = lr\n",
    "config.weight_decay = weight_decay\n",
    "config.epoch = epoch\n",
    "config.bert_name = text_pretrained_model\n",
    "config.resnet_name = cv_pretrained_model\n",
    "config.fuse_model_type = fuse_model_type\n",
    "config.load_model_path = load_model_path\n",
    "config.only = 'img' if img_only else None\n",
    "config.only = 'text' if text_only else None\n",
    "if img_only and text_only: config.only = None\n",
    "print('TextModel: {}, ImageModel: {}, FuseModel: {}'.format(config.bert_name,config.resnet_name, config.fuse_model_type))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e0437635-89b4-471f-83ae-6f7455e767b1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Initilaztion\n",
    "processor = Processor(config)\n",
    "device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "55c1bf06-4c56-41e7-90dc-13c43bf70399",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----- [Loading]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "5395it [00:03, 1408.71it/s]\n",
      "----- [Encoding]: 100%|██████████| 4316/4316 [00:18<00:00, 239.53it/s]\n",
      "----- [Encoding]: 100%|██████████| 1079/1079 [00:04<00:00, 248.72it/s]\n"
     ]
    }
   ],
   "source": [
    "# Dataset\n",
    "# image_path = '../dataset/FundusDataset/Image'\n",
    "image_path = '../../dataset/ODIR-5K/Image'\n",
    "# train_csv_path = '../dataset/FundusDataset/Csv/train_data.csv'\n",
    "# data = read_from_file(train_csv_path,image_path)\n",
    "# train_data, val_data = train_val_split(data)\n",
    "# train_loader = processor(train_data, config.train_params, 'train')\n",
    "# val_loader = processor(val_data, config.val_params, 'train')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dc29a2df-6708-4a3e-845a-b8b14b0d2a9f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# PreTrain\n",
    "# preModel = Model(config)\n",
    "# preTrainer = PreTrainer(config, processor, preModel, device)\n",
    "# best_lr = preTrainer.train_to_get_best_lr(train_loader)\n",
    "# config.learning_rate = best_lr\n",
    "# print('best learning rate: {}'.format(config.learning_rate))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5792737a-6e11-46e5-b4ef-d74f2b9d7361",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Successfully load ckpt ./vssm_tiny_0230_ckpt_epoch_262.pth\n",
      "_IncompatibleKeys(missing_keys=['layers.0.blocks.0.mutli_head_atten.in_proj_weight', 'layers.0.blocks.0.mutli_head_atten.in_proj_bias', 'layers.0.blocks.0.mutli_head_atten.out_proj.weight', 'layers.0.blocks.0.mutli_head_atten.out_proj.bias', 'layers.0.blocks.0.conv33conv33conv11.0.weight', 'layers.0.blocks.0.conv33conv33conv11.0.bias', 'layers.0.blocks.0.conv33conv33conv11.1.weight', 'layers.0.blocks.0.conv33conv33conv11.1.bias', 'layers.0.blocks.0.conv33conv33conv11.1.running_mean', 'layers.0.blocks.0.conv33conv33conv11.1.running_var', 'layers.0.blocks.0.conv33conv33conv11.3.weight', 'layers.0.blocks.0.conv33conv33conv11.3.bias', 'layers.0.blocks.0.conv33conv33conv11.4.weight', 'layers.0.blocks.0.conv33conv33conv11.4.bias', 'layers.0.blocks.0.conv33conv33conv11.4.running_mean', 'layers.0.blocks.0.conv33conv33conv11.4.running_var', 'layers.0.blocks.0.conv33conv33conv11.6.weight', 'layers.0.blocks.0.conv33conv33conv11.6.bias', 'layers.0.blocks.0.sk_conv.conv.0.weight', 'layers.0.blocks.0.sk_conv.conv.0.bias', 'layers.0.blocks.0.sk_conv.conv.1.weight', 'layers.0.blocks.0.sk_conv.conv.1.bias', 'layers.0.blocks.0.sk_conv.conv.2.weight', 'layers.0.blocks.0.sk_conv.conv.2.bias', 'layers.0.blocks.0.sk_conv.conv.3.weight', 'layers.0.blocks.0.sk_conv.conv.3.bias', 'layers.0.blocks.0.sk_conv.fc.0.weight', 'layers.0.blocks.0.sk_conv.fc.0.bias', 'layers.0.blocks.0.sk_conv.fc.2.weight', 'layers.0.blocks.0.sk_conv.fc.2.bias', 'layers.0.blocks.1.mutli_head_atten.in_proj_weight', 'layers.0.blocks.1.mutli_head_atten.in_proj_bias', 'layers.0.blocks.1.mutli_head_atten.out_proj.weight', 'layers.0.blocks.1.mutli_head_atten.out_proj.bias', 'layers.0.blocks.1.conv33conv33conv11.0.weight', 'layers.0.blocks.1.conv33conv33conv11.0.bias', 'layers.0.blocks.1.conv33conv33conv11.1.weight', 'layers.0.blocks.1.conv33conv33conv11.1.bias', 'layers.0.blocks.1.conv33conv33conv11.1.running_mean', 'layers.0.blocks.1.conv33conv33conv11.1.running_var', 'layers.0.blocks.1.conv33conv33conv11.3.weight', 'layers.0.blocks.1.conv33conv33conv11.3.bias', 'layers.0.blocks.1.conv33conv33conv11.4.weight', 'layers.0.blocks.1.conv33conv33conv11.4.bias', 'layers.0.blocks.1.conv33conv33conv11.4.running_mean', 'layers.0.blocks.1.conv33conv33conv11.4.running_var', 'layers.0.blocks.1.conv33conv33conv11.6.weight', 'layers.0.blocks.1.conv33conv33conv11.6.bias', 'layers.0.blocks.1.sk_conv.conv.0.weight', 'layers.0.blocks.1.sk_conv.conv.0.bias', 'layers.0.blocks.1.sk_conv.conv.1.weight', 'layers.0.blocks.1.sk_conv.conv.1.bias', 'layers.0.blocks.1.sk_conv.conv.2.weight', 'layers.0.blocks.1.sk_conv.conv.2.bias', 'layers.0.blocks.1.sk_conv.conv.3.weight', 'layers.0.blocks.1.sk_conv.conv.3.bias', 'layers.0.blocks.1.sk_conv.fc.0.weight', 'layers.0.blocks.1.sk_conv.fc.0.bias', 'layers.0.blocks.1.sk_conv.fc.2.weight', 'layers.0.blocks.1.sk_conv.fc.2.bias', 'layers.1.blocks.0.mutli_head_atten.in_proj_weight', 'layers.1.blocks.0.mutli_head_atten.in_proj_bias', 'layers.1.blocks.0.mutli_head_atten.out_proj.weight', 'layers.1.blocks.0.mutli_head_atten.out_proj.bias', 'layers.1.blocks.0.conv33conv33conv11.0.weight', 'layers.1.blocks.0.conv33conv33conv11.0.bias', 'layers.1.blocks.0.conv33conv33conv11.1.weight', 'layers.1.blocks.0.conv33conv33conv11.1.bias', 'layers.1.blocks.0.conv33conv33conv11.1.running_mean', 'layers.1.blocks.0.conv33conv33conv11.1.running_var', 'layers.1.blocks.0.conv33conv33conv11.3.weight', 'layers.1.blocks.0.conv33conv33conv11.3.bias', 'layers.1.blocks.0.conv33conv33conv11.4.weight', 'layers.1.blocks.0.conv33conv33conv11.4.bias', 'layers.1.blocks.0.conv33conv33conv11.4.running_mean', 'layers.1.blocks.0.conv33conv33conv11.4.running_var', 'layers.1.blocks.0.conv33conv33conv11.6.weight', 'layers.1.blocks.0.conv33conv33conv11.6.bias', 'layers.1.blocks.0.sk_conv.conv.0.weight', 'layers.1.blocks.0.sk_conv.conv.0.bias', 'layers.1.blocks.0.sk_conv.conv.1.weight', 'layers.1.blocks.0.sk_conv.conv.1.bias', 'layers.1.blocks.0.sk_conv.conv.2.weight', 'layers.1.blocks.0.sk_conv.conv.2.bias', 'layers.1.blocks.0.sk_conv.conv.3.weight', 'layers.1.blocks.0.sk_conv.conv.3.bias', 'layers.1.blocks.0.sk_conv.fc.0.weight', 'layers.1.blocks.0.sk_conv.fc.0.bias', 'layers.1.blocks.0.sk_conv.fc.2.weight', 'layers.1.blocks.0.sk_conv.fc.2.bias', 'layers.1.blocks.1.mutli_head_atten.in_proj_weight', 'layers.1.blocks.1.mutli_head_atten.in_proj_bias', 'layers.1.blocks.1.mutli_head_atten.out_proj.weight', 'layers.1.blocks.1.mutli_head_atten.out_proj.bias', 'layers.1.blocks.1.conv33conv33conv11.0.weight', 'layers.1.blocks.1.conv33conv33conv11.0.bias', 'layers.1.blocks.1.conv33conv33conv11.1.weight', 'layers.1.blocks.1.conv33conv33conv11.1.bias', 'layers.1.blocks.1.conv33conv33conv11.1.running_mean', 'layers.1.blocks.1.conv33conv33conv11.1.running_var', 'layers.1.blocks.1.conv33conv33conv11.3.weight', 'layers.1.blocks.1.conv33conv33conv11.3.bias', 'layers.1.blocks.1.conv33conv33conv11.4.weight', 'layers.1.blocks.1.conv33conv33conv11.4.bias', 'layers.1.blocks.1.conv33conv33conv11.4.running_mean', 'layers.1.blocks.1.conv33conv33conv11.4.running_var', 'layers.1.blocks.1.conv33conv33conv11.6.weight', 'layers.1.blocks.1.conv33conv33conv11.6.bias', 'layers.1.blocks.1.sk_conv.conv.0.weight', 'layers.1.blocks.1.sk_conv.conv.0.bias', 'layers.1.blocks.1.sk_conv.conv.1.weight', 'layers.1.blocks.1.sk_conv.conv.1.bias', 'layers.1.blocks.1.sk_conv.conv.2.weight', 'layers.1.blocks.1.sk_conv.conv.2.bias', 'layers.1.blocks.1.sk_conv.conv.3.weight', 'layers.1.blocks.1.sk_conv.conv.3.bias', 'layers.1.blocks.1.sk_conv.fc.0.weight', 'layers.1.blocks.1.sk_conv.fc.0.bias', 'layers.1.blocks.1.sk_conv.fc.2.weight', 'layers.1.blocks.1.sk_conv.fc.2.bias', 'layers.2.blocks.0.mutli_head_atten.in_proj_weight', 'layers.2.blocks.0.mutli_head_atten.in_proj_bias', 'layers.2.blocks.0.mutli_head_atten.out_proj.weight', 'layers.2.blocks.0.mutli_head_atten.out_proj.bias', 'layers.2.blocks.0.conv33conv33conv11.0.weight', 'layers.2.blocks.0.conv33conv33conv11.0.bias', 'layers.2.blocks.0.conv33conv33conv11.1.weight', 'layers.2.blocks.0.conv33conv33conv11.1.bias', 'layers.2.blocks.0.conv33conv33conv11.1.running_mean', 'layers.2.blocks.0.conv33conv33conv11.1.running_var', 'layers.2.blocks.0.conv33conv33conv11.3.weight', 'layers.2.blocks.0.conv33conv33conv11.3.bias', 'layers.2.blocks.0.conv33conv33conv11.4.weight', 'layers.2.blocks.0.conv33conv33conv11.4.bias', 'layers.2.blocks.0.conv33conv33conv11.4.running_mean', 'layers.2.blocks.0.conv33conv33conv11.4.running_var', 'layers.2.blocks.0.conv33conv33conv11.6.weight', 'layers.2.blocks.0.conv33conv33conv11.6.bias', 'layers.2.blocks.0.sk_conv.conv.0.weight', 'layers.2.blocks.0.sk_conv.conv.0.bias', 'layers.2.blocks.0.sk_conv.conv.1.weight', 'layers.2.blocks.0.sk_conv.conv.1.bias', 'layers.2.blocks.0.sk_conv.conv.2.weight', 'layers.2.blocks.0.sk_conv.conv.2.bias', 'layers.2.blocks.0.sk_conv.conv.3.weight', 'layers.2.blocks.0.sk_conv.conv.3.bias', 'layers.2.blocks.0.sk_conv.fc.0.weight', 'layers.2.blocks.0.sk_conv.fc.0.bias', 'layers.2.blocks.0.sk_conv.fc.2.weight', 'layers.2.blocks.0.sk_conv.fc.2.bias', 'layers.2.blocks.1.mutli_head_atten.in_proj_weight', 'layers.2.blocks.1.mutli_head_atten.in_proj_bias', 'layers.2.blocks.1.mutli_head_atten.out_proj.weight', 'layers.2.blocks.1.mutli_head_atten.out_proj.bias', 'layers.2.blocks.1.conv33conv33conv11.0.weight', 'layers.2.blocks.1.conv33conv33conv11.0.bias', 'layers.2.blocks.1.conv33conv33conv11.1.weight', 'layers.2.blocks.1.conv33conv33conv11.1.bias', 'layers.2.blocks.1.conv33conv33conv11.1.running_mean', 'layers.2.blocks.1.conv33conv33conv11.1.running_var', 'layers.2.blocks.1.conv33conv33conv11.3.weight', 'layers.2.blocks.1.conv33conv33conv11.3.bias', 'layers.2.blocks.1.conv33conv33conv11.4.weight', 'layers.2.blocks.1.conv33conv33conv11.4.bias', 'layers.2.blocks.1.conv33conv33conv11.4.running_mean', 'layers.2.blocks.1.conv33conv33conv11.4.running_var', 'layers.2.blocks.1.conv33conv33conv11.6.weight', 'layers.2.blocks.1.conv33conv33conv11.6.bias', 'layers.2.blocks.1.sk_conv.conv.0.weight', 'layers.2.blocks.1.sk_conv.conv.0.bias', 'layers.2.blocks.1.sk_conv.conv.1.weight', 'layers.2.blocks.1.sk_conv.conv.1.bias', 'layers.2.blocks.1.sk_conv.conv.2.weight', 'layers.2.blocks.1.sk_conv.conv.2.bias', 'layers.2.blocks.1.sk_conv.conv.3.weight', 'layers.2.blocks.1.sk_conv.conv.3.bias', 'layers.2.blocks.1.sk_conv.fc.0.weight', 'layers.2.blocks.1.sk_conv.fc.0.bias', 'layers.2.blocks.1.sk_conv.fc.2.weight', 'layers.2.blocks.1.sk_conv.fc.2.bias', 'layers.2.blocks.2.mutli_head_atten.in_proj_weight', 'layers.2.blocks.2.mutli_head_atten.in_proj_bias', 'layers.2.blocks.2.mutli_head_atten.out_proj.weight', 'layers.2.blocks.2.mutli_head_atten.out_proj.bias', 'layers.2.blocks.2.conv33conv33conv11.0.weight', 'layers.2.blocks.2.conv33conv33conv11.0.bias', 'layers.2.blocks.2.conv33conv33conv11.1.weight', 'layers.2.blocks.2.conv33conv33conv11.1.bias', 'layers.2.blocks.2.conv33conv33conv11.1.running_mean', 'layers.2.blocks.2.conv33conv33conv11.1.running_var', 'layers.2.blocks.2.conv33conv33conv11.3.weight', 'layers.2.blocks.2.conv33conv33conv11.3.bias', 'layers.2.blocks.2.conv33conv33conv11.4.weight', 'layers.2.blocks.2.conv33conv33conv11.4.bias', 'layers.2.blocks.2.conv33conv33conv11.4.running_mean', 'layers.2.blocks.2.conv33conv33conv11.4.running_var', 'layers.2.blocks.2.conv33conv33conv11.6.weight', 'layers.2.blocks.2.conv33conv33conv11.6.bias', 'layers.2.blocks.2.sk_conv.conv.0.weight', 'layers.2.blocks.2.sk_conv.conv.0.bias', 'layers.2.blocks.2.sk_conv.conv.1.weight', 'layers.2.blocks.2.sk_conv.conv.1.bias', 'layers.2.blocks.2.sk_conv.conv.2.weight', 'layers.2.blocks.2.sk_conv.conv.2.bias', 'layers.2.blocks.2.sk_conv.conv.3.weight', 'layers.2.blocks.2.sk_conv.conv.3.bias', 'layers.2.blocks.2.sk_conv.fc.0.weight', 'layers.2.blocks.2.sk_conv.fc.0.bias', 'layers.2.blocks.2.sk_conv.fc.2.weight', 'layers.2.blocks.2.sk_conv.fc.2.bias', 'layers.2.blocks.3.mutli_head_atten.in_proj_weight', 'layers.2.blocks.3.mutli_head_atten.in_proj_bias', 'layers.2.blocks.3.mutli_head_atten.out_proj.weight', 'layers.2.blocks.3.mutli_head_atten.out_proj.bias', 'layers.2.blocks.3.conv33conv33conv11.0.weight', 'layers.2.blocks.3.conv33conv33conv11.0.bias', 'layers.2.blocks.3.conv33conv33conv11.1.weight', 'layers.2.blocks.3.conv33conv33conv11.1.bias', 'layers.2.blocks.3.conv33conv33conv11.1.running_mean', 'layers.2.blocks.3.conv33conv33conv11.1.running_var', 'layers.2.blocks.3.conv33conv33conv11.3.weight', 'layers.2.blocks.3.conv33conv33conv11.3.bias', 'layers.2.blocks.3.conv33conv33conv11.4.weight', 'layers.2.blocks.3.conv33conv33conv11.4.bias', 'layers.2.blocks.3.conv33conv33conv11.4.running_mean', 'layers.2.blocks.3.conv33conv33conv11.4.running_var', 'layers.2.blocks.3.conv33conv33conv11.6.weight', 'layers.2.blocks.3.conv33conv33conv11.6.bias', 'layers.2.blocks.3.sk_conv.conv.0.weight', 'layers.2.blocks.3.sk_conv.conv.0.bias', 'layers.2.blocks.3.sk_conv.conv.1.weight', 'layers.2.blocks.3.sk_conv.conv.1.bias', 'layers.2.blocks.3.sk_conv.conv.2.weight', 'layers.2.blocks.3.sk_conv.conv.2.bias', 'layers.2.blocks.3.sk_conv.conv.3.weight', 'layers.2.blocks.3.sk_conv.conv.3.bias', 'layers.2.blocks.3.sk_conv.fc.0.weight', 'layers.2.blocks.3.sk_conv.fc.0.bias', 'layers.2.blocks.3.sk_conv.fc.2.weight', 'layers.2.blocks.3.sk_conv.fc.2.bias', 'layers.2.blocks.4.mutli_head_atten.in_proj_weight', 'layers.2.blocks.4.mutli_head_atten.in_proj_bias', 'layers.2.blocks.4.mutli_head_atten.out_proj.weight', 'layers.2.blocks.4.mutli_head_atten.out_proj.bias', 'layers.2.blocks.4.conv33conv33conv11.0.weight', 'layers.2.blocks.4.conv33conv33conv11.0.bias', 'layers.2.blocks.4.conv33conv33conv11.1.weight', 'layers.2.blocks.4.conv33conv33conv11.1.bias', 'layers.2.blocks.4.conv33conv33conv11.1.running_mean', 'layers.2.blocks.4.conv33conv33conv11.1.running_var', 'layers.2.blocks.4.conv33conv33conv11.3.weight', 'layers.2.blocks.4.conv33conv33conv11.3.bias', 'layers.2.blocks.4.conv33conv33conv11.4.weight', 'layers.2.blocks.4.conv33conv33conv11.4.bias', 'layers.2.blocks.4.conv33conv33conv11.4.running_mean', 'layers.2.blocks.4.conv33conv33conv11.4.running_var', 'layers.2.blocks.4.conv33conv33conv11.6.weight', 'layers.2.blocks.4.conv33conv33conv11.6.bias', 'layers.2.blocks.4.sk_conv.conv.0.weight', 'layers.2.blocks.4.sk_conv.conv.0.bias', 'layers.2.blocks.4.sk_conv.conv.1.weight', 'layers.2.blocks.4.sk_conv.conv.1.bias', 'layers.2.blocks.4.sk_conv.conv.2.weight', 'layers.2.blocks.4.sk_conv.conv.2.bias', 'layers.2.blocks.4.sk_conv.conv.3.weight', 'layers.2.blocks.4.sk_conv.conv.3.bias', 'layers.2.blocks.4.sk_conv.fc.0.weight', 'layers.2.blocks.4.sk_conv.fc.0.bias', 'layers.2.blocks.4.sk_conv.fc.2.weight', 'layers.2.blocks.4.sk_conv.fc.2.bias', 'layers.3.blocks.0.mutli_head_atten.in_proj_weight', 'layers.3.blocks.0.mutli_head_atten.in_proj_bias', 'layers.3.blocks.0.mutli_head_atten.out_proj.weight', 'layers.3.blocks.0.mutli_head_atten.out_proj.bias', 'layers.3.blocks.0.conv33conv33conv11.0.weight', 'layers.3.blocks.0.conv33conv33conv11.0.bias', 'layers.3.blocks.0.conv33conv33conv11.1.weight', 'layers.3.blocks.0.conv33conv33conv11.1.bias', 'layers.3.blocks.0.conv33conv33conv11.1.running_mean', 'layers.3.blocks.0.conv33conv33conv11.1.running_var', 'layers.3.blocks.0.conv33conv33conv11.3.weight', 'layers.3.blocks.0.conv33conv33conv11.3.bias', 'layers.3.blocks.0.conv33conv33conv11.4.weight', 'layers.3.blocks.0.conv33conv33conv11.4.bias', 'layers.3.blocks.0.conv33conv33conv11.4.running_mean', 'layers.3.blocks.0.conv33conv33conv11.4.running_var', 'layers.3.blocks.0.conv33conv33conv11.6.weight', 'layers.3.blocks.0.conv33conv33conv11.6.bias', 'layers.3.blocks.0.sk_conv.conv.0.weight', 'layers.3.blocks.0.sk_conv.conv.0.bias', 'layers.3.blocks.0.sk_conv.conv.1.weight', 'layers.3.blocks.0.sk_conv.conv.1.bias', 'layers.3.blocks.0.sk_conv.conv.2.weight', 'layers.3.blocks.0.sk_conv.conv.2.bias', 'layers.3.blocks.0.sk_conv.conv.3.weight', 'layers.3.blocks.0.sk_conv.conv.3.bias', 'layers.3.blocks.0.sk_conv.fc.0.weight', 'layers.3.blocks.0.sk_conv.fc.0.bias', 'layers.3.blocks.0.sk_conv.fc.2.weight', 'layers.3.blocks.0.sk_conv.fc.2.bias', 'layers.3.blocks.1.mutli_head_atten.in_proj_weight', 'layers.3.blocks.1.mutli_head_atten.in_proj_bias', 'layers.3.blocks.1.mutli_head_atten.out_proj.weight', 'layers.3.blocks.1.mutli_head_atten.out_proj.bias', 'layers.3.blocks.1.conv33conv33conv11.0.weight', 'layers.3.blocks.1.conv33conv33conv11.0.bias', 'layers.3.blocks.1.conv33conv33conv11.1.weight', 'layers.3.blocks.1.conv33conv33conv11.1.bias', 'layers.3.blocks.1.conv33conv33conv11.1.running_mean', 'layers.3.blocks.1.conv33conv33conv11.1.running_var', 'layers.3.blocks.1.conv33conv33conv11.3.weight', 'layers.3.blocks.1.conv33conv33conv11.3.bias', 'layers.3.blocks.1.conv33conv33conv11.4.weight', 'layers.3.blocks.1.conv33conv33conv11.4.bias', 'layers.3.blocks.1.conv33conv33conv11.4.running_mean', 'layers.3.blocks.1.conv33conv33conv11.4.running_var', 'layers.3.blocks.1.conv33conv33conv11.6.weight', 'layers.3.blocks.1.conv33conv33conv11.6.bias', 'layers.3.blocks.1.sk_conv.conv.0.weight', 'layers.3.blocks.1.sk_conv.conv.0.bias', 'layers.3.blocks.1.sk_conv.conv.1.weight', 'layers.3.blocks.1.sk_conv.conv.1.bias', 'layers.3.blocks.1.sk_conv.conv.2.weight', 'layers.3.blocks.1.sk_conv.conv.2.bias', 'layers.3.blocks.1.sk_conv.conv.3.weight', 'layers.3.blocks.1.sk_conv.conv.3.bias', 'layers.3.blocks.1.sk_conv.fc.0.weight', 'layers.3.blocks.1.sk_conv.fc.0.bias', 'layers.3.blocks.1.sk_conv.fc.2.weight', 'layers.3.blocks.1.sk_conv.fc.2.bias'], unexpected_keys=[])\n"
     ]
    }
   ],
   "source": [
    "model = Model(config)\n",
    "trainer = Trainer(config, processor, model, device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4ce90444-d574-49b7-8514-f7d92ca47872",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Train\n",
    "def train():\n",
    "    best_acc = 0\n",
    "    epoch = config.epoch\n",
    "    for e in range(epoch):\n",
    "        print('-' * 20 + ' ' + 'Epoch ' + str(e+1) + ' ' + '-' * 20)\n",
    "        tloss = trainer.train(train_loader)\n",
    "        print('Train Loss: {}'.format(tloss))\n",
    "        vloss, vacc = trainer.valid(val_loader)\n",
    "        print('Valid Loss: {}'.format(vloss))\n",
    "        print('Valid Acc: {}'.format(vacc))\n",
    "        if vacc > best_acc:\n",
    "            best_acc = vacc\n",
    "            save_path = './save_models'\n",
    "            save_model(save_path, config.fuse_model_type, model)\n",
    "            print('Update best model!')\n",
    "        print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4039ee07-6bad-4175-a79b-09c2cc9866fd",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Test\n",
    "def test():\n",
    "    # test_csv_path = '../../dataset/FundusDataset/Csv/test_data.csv'\n",
    "    test_csv_path = '../../dataset/ODIR-5K/Csv/Label.csv'\n",
    "    test_data = read_from_file(test_csv_path,image_path)\n",
    "    test_loader = processor(test_data, config.test_params,'test')\n",
    "\n",
    "    if config.load_model_path is not None:\n",
    "        model.load_state_dict(torch.load(config.load_model_path))\n",
    "\n",
    "    outputs = trainer.predict(test_loader)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7f20f670-9076-4fa1-85f3-f946b1b59665",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "----- [Training] : 100%|██████████| 1079/1079 [02:52<00:00,  6.27it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "------------------Per Class Metrics--------------------\n",
      "  Class  Accuracy Rate  Precision  Recall  F1-score     AUC\n",
      "0     N         0.9314     0.7428  0.9755    0.8434  0.9834\n",
      "1     D         0.8239     0.5772  0.9389    0.7149  0.9434\n",
      "2     G         0.9627     0.8212  0.9506    0.8812  0.9771\n",
      "3     C         0.9859     0.9220  0.9837    0.9519  0.9945\n",
      "4     A         0.9537     0.7203  0.9406    0.8158  0.9781\n",
      "5     H         0.9511     0.6943  0.9489    0.8019  0.9798\n",
      "6     M         0.9898     0.9260  0.9942    0.9589  0.9979\n",
      "7     O         0.7933     0.5801  0.9402    0.7175  0.9329\n",
      "\n",
      "Kappa Score: 0.7605\n",
      "Average AUC: 0.9734\n",
      "-------------------------------------------------------\n",
      "acc= 0.924 f1= 0.8357 precision= 0.748 recall= 0.9591\n",
      "Train Loss: 1.5504\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\t ----- [Validing] : 100%|██████████| 270/270 [00:21<00:00, 12.29it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "------------------Per Class Metrics--------------------\n",
      "  Class  Accuracy Rate  Precision  Recall  F1-score     AUC\n",
      "0     N         0.8285     0.5143  0.6562    0.5767  0.7961\n",
      "1     D         0.7247     0.4600  0.7197    0.5613  0.7813\n",
      "2     G         0.8842     0.5955  0.7844    0.6770  0.8811\n",
      "3     C         0.9518     0.7964  0.8808    0.8365  0.9490\n",
      "4     A         0.8610     0.4316  0.6613    0.5223  0.8312\n",
      "5     H         0.9166     0.5695  0.7748    0.6565  0.8965\n",
      "6     M         0.9694     0.8085  0.9500    0.8736  0.9760\n",
      "7     O         0.6052     0.4171  0.6647    0.5126  0.6833\n",
      "\n",
      "Kappa Score: 0.52\n",
      "Average AUC: 0.8493\n",
      "-------------------------------------------------------\n",
      "acc= 0.8427 f1= 0.6521 precision= 0.5741 recall= 0.7615\n",
      "Valid Loss: 3.2958023921207147\n",
      "Valid Acc: 0.8427\n",
      "Update best model!\n",
      "\n",
      "-------------------- Epoch 16 --------------------\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "----- [Training] : 100%|██████████| 1079/1079 [02:52<00:00,  6.27it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "------------------Per Class Metrics--------------------\n",
      "  Class  Accuracy Rate  Precision  Recall  F1-score     AUC\n",
      "0     N         0.9428     0.7794  0.9731    0.8655  0.9857\n",
      "1     D         0.8390     0.6003  0.9438    0.7338  0.9545\n",
      "2     G         0.9669     0.8354  0.9618    0.8942  0.9844\n",
      "3     C         0.9852     0.9153  0.9869    0.9498  0.9962\n",
      "4     A         0.9604     0.7595  0.9321    0.8370  0.9717\n",
      "5     H         0.9555     0.7232  0.9289    0.8132  0.9836\n",
      "6     M         0.9900     0.9308  0.9903    0.9596  0.9978\n",
      "7     O         0.8181     0.6125  0.9485    0.7444  0.9466\n",
      "\n",
      "Kappa Score: 0.7829\n",
      "Average AUC: 0.9776\n",
      "-------------------------------------------------------\n",
      "acc= 0.9322 f1= 0.8497 precision= 0.7696 recall= 0.9582\n",
      "Train Loss: 1.458\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\t ----- [Validing] : 100%|██████████| 270/270 [00:21<00:00, 12.49it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "------------------Per Class Metrics--------------------\n",
      "  Class  Accuracy Rate  Precision  Recall  F1-score     AUC\n",
      "0     N         0.8063     0.4721  0.7500    0.5795  0.8353\n",
      "1     D         0.7600     0.5069  0.6932    0.5856  0.7756\n",
      "2     G         0.9027     0.6667  0.7425    0.7025  0.8737\n",
      "3     C         0.9555     0.8199  0.8742    0.8462  0.9560\n",
      "4     A         0.8665     0.4474  0.6855    0.5414  0.8210\n",
      "5     H         0.8925     0.4865  0.8108    0.6081  0.9078\n",
      "6     M         0.9722     0.8214  0.9583    0.8846  0.9846\n",
      "7     O         0.6617     0.4653  0.5579    0.5074  0.6654\n",
      "\n",
      "Kappa Score: 0.5346\n",
      "Average AUC: 0.8524\n",
      "-------------------------------------------------------\n",
      "acc= 0.8522 f1= 0.6569 precision= 0.5858 recall= 0.759\n",
      "Valid Loss: 3.326657827253695\n",
      "Valid Acc: 0.8522\n",
      "Update best model!\n",
      "\n",
      "-------------------- Epoch 17 --------------------\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "----- [Training] : 100%|██████████| 1079/1079 [02:51<00:00,  6.30it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "------------------Per Class Metrics--------------------\n",
      "  Class  Accuracy Rate  Precision  Recall  F1-score     AUC\n",
      "0     N         0.9474     0.7915  0.9804    0.8759  0.9916\n",
      "1     D         0.8526     0.6230  0.9458    0.7512  0.9570\n",
      "2     G         0.9701     0.8519  0.9618    0.9035  0.9829\n",
      "3     C         0.9905     0.9497  0.9853    0.9672  0.9931\n",
      "4     A         0.9648     0.7823  0.9384    0.8533  0.9731\n",
      "5     H         0.9639     0.7561  0.9644    0.8477  0.9850\n",
      "6     M         0.9905     0.9342  0.9903    0.9614  0.9958\n",
      "7     O         0.8371     0.6391  0.9568    0.7664  0.9609\n",
      "\n",
      "Kappa Score: 0.8044\n",
      "Average AUC: 0.9799\n",
      "-------------------------------------------------------\n",
      "acc= 0.9396 f1= 0.8658 precision= 0.791 recall= 0.9654\n",
      "Train Loss: 1.3857\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\t ----- [Validing] : 100%|██████████| 270/270 [00:21<00:00, 12.64it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "------------------Per Class Metrics--------------------\n",
      "  Class  Accuracy Rate  Precision  Recall  F1-score     AUC\n",
      "0     N         0.8267     0.5105  0.6302    0.5641  0.8046\n",
      "1     D         0.7136     0.4460  0.7045    0.5463  0.7706\n",
      "2     G         0.9036     0.6760  0.7246    0.6994  0.8711\n",
      "3     C         0.9490     0.7697  0.9073    0.8328  0.9648\n",
      "4     A         0.8610     0.4356  0.7097    0.5399  0.8294\n",
      "5     H         0.9212     0.5915  0.7568    0.6640  0.8877\n",
      "6     M         0.9731     0.8321  0.9500    0.8872  0.9765\n",
      "7     O         0.6321     0.4383  0.6320    0.5176  0.6893\n",
      "\n",
      "Kappa Score: 0.5253\n",
      "Average AUC: 0.8493\n",
      "-------------------------------------------------------\n",
      "acc= 0.8475 f1= 0.6564 precision= 0.5875 recall= 0.7519\n",
      "Valid Loss: 3.4541786313056946\n",
      "Valid Acc: 0.8475\n",
      "\n",
      "-------------------- Epoch 18 --------------------\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "----- [Training] : 100%|██████████| 1079/1079 [02:50<00:00,  6.31it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "------------------Per Class Metrics--------------------\n",
      "  Class  Accuracy Rate  Precision  Recall  F1-score     AUC\n",
      "0     N         0.9576     0.8282  0.9792    0.8974  0.9919\n",
      "1     D         0.8728     0.6581  0.9557    0.7794  0.9687\n",
      "2     G         0.9741     0.8728  0.9618    0.9152  0.9827\n",
      "3     C         0.9875     0.9241  0.9935    0.9575  0.9972\n",
      "4     A         0.9625     0.7706  0.9342    0.8445  0.9762\n",
      "5     H         0.9659     0.7681  0.9644    0.8552  0.9894\n",
      "6     M         0.9898     0.9275  0.9922    0.9588  0.9959\n",
      "7     O         0.8550     0.6676  0.9568    0.7865  0.9636\n",
      "\n",
      "Kappa Score: 0.8218\n",
      "Average AUC: 0.9832\n",
      "-------------------------------------------------------\n",
      "acc= 0.9456 f1= 0.8743 precision= 0.8021 recall= 0.9672\n",
      "Train Loss: 1.3221\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\t ----- [Validing] : 100%|██████████| 270/270 [00:22<00:00, 12.17it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "------------------Per Class Metrics--------------------\n",
      "  Class  Accuracy Rate  Precision  Recall  F1-score     AUC\n",
      "0     N         0.7961     0.4576  0.7865    0.5785  0.8407\n",
      "1     D         0.7859     0.5563  0.6174    0.5853  0.7794\n",
      "2     G         0.9008     0.6579  0.7485    0.7003  0.8766\n",
      "3     C         0.9509     0.7692  0.9272    0.8408  0.9644\n",
      "4     A         0.8823     0.4903  0.6129    0.5448  0.8223\n",
      "5     H         0.8971     0.5000  0.8108    0.6186  0.9078\n",
      "6     M         0.9759     0.8561  0.9417    0.8968  0.9728\n",
      "7     O         0.6673     0.4715  0.5401    0.5035  0.6687\n",
      "\n",
      "Kappa Score: 0.5406\n",
      "Average AUC: 0.8541\n",
      "-------------------------------------------------------\n",
      "acc= 0.857 f1= 0.6586 precision= 0.5949 recall= 0.7481\n",
      "Valid Loss: 3.5059910893440245\n",
      "Valid Acc: 0.857\n",
      "Update best model!\n",
      "\n",
      "-------------------- Epoch 19 --------------------\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "----- [Training] : 100%|██████████| 1079/1079 [02:53<00:00,  6.23it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "------------------Per Class Metrics--------------------\n",
      "  Class  Accuracy Rate  Precision  Recall  F1-score     AUC\n",
      "0     N         0.9509     0.8034  0.9804    0.8831  0.9918\n",
      "1     D         0.8809     0.6741  0.9557    0.7905  0.9686\n",
      "2     G         0.9729     0.8645  0.9650    0.9120  0.9868\n",
      "3     C         0.9893     0.9382  0.9902    0.9635  0.9963\n",
      "4     A         0.9701     0.8087  0.9512    0.8741  0.9827\n",
      "5     H         0.9662     0.7744  0.9533    0.8546  0.9902\n",
      "6     M         0.9926     0.9465  0.9942    0.9698  0.9970\n",
      "7     O         0.8547     0.6676  0.9552    0.7859  0.9672\n",
      "\n",
      "Kappa Score: 0.8264\n",
      "Average AUC: 0.9851\n",
      "-------------------------------------------------------\n",
      "acc= 0.9472 f1= 0.8792 precision= 0.8097 recall= 0.9682\n",
      "Train Loss: 1.2973\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\t ----- [Validing] : 100%|██████████| 270/270 [00:21<00:00, 12.53it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "------------------Per Class Metrics--------------------\n",
      "  Class  Accuracy Rate  Precision  Recall  F1-score     AUC\n",
      "0     N         0.8156     0.4866  0.6615    0.5607  0.7921\n",
      "1     D         0.7322     0.4704  0.7538    0.5793  0.7957\n",
      "2     G         0.8916     0.6147  0.8024    0.6961  0.8805\n",
      "3     C         0.9351     0.6976  0.9470    0.8034  0.9642\n",
      "4     A         0.8888     0.5130  0.6371    0.5683  0.8218\n",
      "5     H         0.9073     0.5314  0.8378    0.6503  0.9113\n",
      "6     M         0.9713     0.8296  0.9333    0.8784  0.9760\n",
      "7     O         0.6627     0.4695  0.6172    0.5333  0.6876\n",
      "\n",
      "Kappa Score: 0.5386\n",
      "Average AUC: 0.8536\n",
      "-------------------------------------------------------\n",
      "acc= 0.8506 f1= 0.6587 precision= 0.5766 recall= 0.7738\n",
      "Valid Loss: 3.6285769835666373\n",
      "Valid Acc: 0.8506\n",
      "\n",
      "-------------------- Epoch 20 --------------------\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "----- [Training] :  49%|████▉     | 532/1079 [01:27<01:21,  6.68it/s]Exception ignored in: <function _MultiProcessingDataLoaderIter.__del__ at 0x7fd0ab502c10>\n",
      "Traceback (most recent call last):\n",
      "  File \"/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py\", line 1478, in __del__\n",
      "    self._shutdown_workers()\n",
      "  File \"/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py\", line 1442, in _shutdown_workers\n",
      "    w.join(timeout=_utils.MP_STATUS_CHECK_INTERVAL)\n",
      "  File \"/root/miniconda3/lib/python3.8/multiprocessing/process.py\", line 149, in join\n",
      "    res = self._popen.wait(timeout)\n",
      "  File \"/root/miniconda3/lib/python3.8/multiprocessing/popen_fork.py\", line 44, in wait\n",
      "    if not wait([self.sentinel], timeout):\n",
      "  File \"/root/miniconda3/lib/python3.8/multiprocessing/connection.py\", line 931, in wait\n",
      "    ready = selector.select(timeout)\n",
      "  File \"/root/miniconda3/lib/python3.8/selectors.py\", line 415, in select\n",
      "    fd_event_list = self._selector.poll(timeout)\n",
      "KeyboardInterrupt: \n",
      "----- [Training] :  49%|████▉     | 532/1079 [01:27<01:29,  6.08it/s]\n",
      "\n",
      "KeyboardInterrupt\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# main\n",
    "if __name__ == \"__main__\":\n",
    "    if do_train:\n",
    "        train()\n",
    "    \n",
    "    if do_test:\n",
    "        if load_model_path is None and not do_train:\n",
    "            print('请输入已训练好模型的路径load_model_path或者选择添加do_train arg')\n",
    "        else:\n",
    "            test()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0e1abb2-2179-410d-bad4-3c906d0c842d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14a52e37-cb84-4de8-86cb-381c131d9152",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff430f27-42fe-41cb-91dc-150d71f582bf",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
